package me.jzn.alib.drawable;

import android.graphics.drawable.Drawable;
import android.view.View;
import android.widget.ImageView;
import android.widget.RemoteViews;

import androidx.annotation.IdRes;

import java.io.File;

public class FileDrawableType implements DrawableType {
    private File file;

    public FileDrawableType(File file) {
        this.file = file;
    }

    public File getFile() {
        return file;
    }

    @Override
    public Drawable getDrawable() {
        String filePath = file.getAbsolutePath();
        //因为如果是file的时候，Icon.loadDrawable会爆出资源泄露。
        //因此，无论是getDrawable还是ImageView.setImage,都会资源泄露.
        //因此，就不用IconDrawableType来表示了，统一用DefaultDrawableType来表示吧
        return Drawable.createFromPath(filePath);
//        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
//
//            return new IconDrawableType(Icon.createWithFilePath(filePath));
//        } else {
//            return new DefaultDrawableType(Drawable.createFromPath(filePath));
//        }
    }

    @Override
    public void setImageView(ImageView iv) {
        iv.setImageDrawable(Drawable.createFromPath(file.getAbsolutePath()));
    }

    @Override
    public void setBackground(View view) {
        view.setBackground(Drawable.createFromPath(file.getAbsolutePath()));
    }

    @Deprecated
    @Override
    public void setRemoteViews(RemoteViews rv, @IdRes int imageViewId) {
        throw new UnsupportedOperationException("remoteviews不支持file类型");
    }
}
